var BSTYUN = BSTYUN || {};
//全局弹窗
BSTYUN.dialog = {
	    close: function (id) {
	        var i,list = $.dialog.list;
	        if(id){
	            list[id].close();
	        }else{
	            for (i in list) {
	                list[i].close();
	            }
	        }
	    },
	    creat: function (options) {
	        var dialog = null;
	        options = $.extend({
	            fixed:true,
	            title:false,
	            lock:true,
	            padding:'20px 40px',
	            id: '',
	            content: ''
	        },options);
	        dialog=$.dialog(options);
	        return dialog;
	    },
	    success: function (html,time) {
	        var dialog = null;
	        time = time || 2;
	        this.close();
	        html = '<div class="success-tip"><i class="iconfont">&#379;</i>'+html+'</div>'
	        dialog = this.creat({id:'success',content:html,lock:false});
	        setTimeout(function(){
	            var $dg = $(dialog.DOM.wrap);
	            $dg.animate({'top':'-=50px','opacity':0},300,'easeInBack',function(){
	                dialog.close();
	            });
	        }, time*1000);
	    },
	    warn: function (html,callback){
	        var dialog = null;
	        html = '<div class="warn-tip"><i class="iconfont">&#227;</i>'+html+'</div>';
	        dialog = this.creat({id:'warn',content:html});
	        dialog.button({
	            name: '知道了',
	            focus: true,
	            callback: callback
	        });
	    },
	    ok: function (html,callback){
	        var dialog = null;
	        html = '<div class="ok-tip"><i class="iconfont">&#379;</i>'+html+'</div>';
	        dialog = this.creat({id:'ok',content:html});
	        dialog.button({
	            name: '同意',
	            focus: true,
	            callback: callback
	        });
	    },
	    requestContent: function(url,callback){
	        var dialog = null;
	          dialog = $.dialog({
	                fixed:true,
	                title:false,
	                lock:true,
	                padding:'20px 40px',
	                id: 'requestContent',
	                content: undefined
	          });
	          $.ajax({
	              url: url,
	              type: 'get',
	              dataType: 'text',
	              async: false,
	              success:function(html){
	                dialog.content(html);
	                dialog.button([{
	                    name: '同意',
	                    focus: true,
	                    callback: callback
	                    }]);
	              },
	              error: function(error){
	            	  BSTYUN.dialog.ok(error)
	              }
	            });
	        },
	    confirm: function (html,callback) {
	        var dialog = null;
	        html = '<div class="confirm-tip"><i class="iconfont">&#228;</i>'+html+'</div>';
	        dialog = this.creat({id:'confirm',content:html});
	        dialog.button({
	            name: '确定',
	            focus: true,
	            callback: callback
	        },{
	            name: '取消'
	        });
	    }
};

/**
 * 登录表单提交
 */
function onLogin() {
	setTimeout(function(){
		var userEmail = $("#userEmail").val();
		var userPassword = $("#userPassword").val();
		if(userEmail==""){
			$("#errorInfo span").html("<img src='/images/user/users/register/cuowu.png'  style='vertical-align:middle;'>&nbsp;请输入帐户名");
			$("#errorInfo").show();
			return
		}
		$("#errorInfo").hide();
		if(userPassword==""){
			$("#errorInfo span").html("<img src='/images/user/users/register/cuowu.png'  style='vertical-align:middle;'>&nbsp;请输入密码");
			$("#errorInfo").show();
			return
		}
		$("#errorInfo").hide();
		$.ajax({
			cache : true,
			type : "POST",
			url : "/user/loginCheck?random="+Math.random(),
			data : {userEmail:userEmail,userPassword:userPassword},
			async : true,
			error : function(request) {
				$("#errorInfo span").html("<img src='/images/user/users/register/cuowu.png'  style='vertical-align:middle;'>&nbsp;登录失败！请重试");
			},
			success : function(data) {
				var loginMessage = data.loginMessage;
				var isyun = data.isyun;
				var isFirstNum = data.isFirstLogin;
				if(loginMessage=="success"){
					if(isyun=="yes"){
						$("#userForm").attr("action","/user/userLogin").submit();
					}else{
						//判断是否同意注册协议
						if(isFirstNum=="yes"){
							BSTYUN.dialog.requestContent('/user/py/openBstDeal',function(){
								$("#isFirstLogin").val("yes");;
								$("#userForm").attr("action","/user/userLogin").submit();
							});
						}else{
							$("#userForm").attr("action","/user/userLogin").submit();
						}
					}
					
				}else{
					$("#errorInfo span").html("<img src='/images/user/users/register/cuowu.png'  style='vertical-align:middle;'>&nbsp;"+loginMessage);
					$("#errorInfo").show();
					$("#userPassword").val("");
					
				}
				
			}
		})
	},100)
}

/**
 * 敲击回车键提交表单
 */
function keyLogin(){
	if(event.keyCode == 13){
		onLogin();
	}
}

//判断浏览器是否支持placeholder属性
function isPlaceholder(){
     var input = document.createElement('input');
     return 'placeholder' in input;
};

//不支持Placeholder()的浏览器
if(!isPlaceholder()){ 
     $(function(){
          var $user = $("#userEmail"),
          	  $psd = $("#userPassword");
          $user.parent().prepend($("<span class='iconfont loginicon'>&#xe612;</span>").css({"font-size":"18px","margin-left":"10px","color":"#ccc"})).end().css({"width":"280px"});
          $psd.parent().prepend($("<span class='iconfont loginicon'>&#xe606;</span>").css({"font-size":"18px","margin-left":"10px","color":"#ccc"})).end().css({"width":"280px"});
     });
};
